package com.example.rk.service.impl;

import com.example.rk.common.MyPageInfo;
import com.example.rk.entity.*;
import com.example.rk.mapper.ChoiceMapper;
import com.example.rk.service.ChoiceService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

@Service
public class ChoiceServiceImpl implements ChoiceService {
    @Autowired
    private ChoiceMapper adminMapper;


    @Override
    public String doAdd(Choice choice) {
       Choice db=adminMapper.findadd(choice.getTitle(),choice.getType());
        if (db==null){
            adminMapper.create(choice);
            return "ok";
        }else{
            return "exist";
        }
    }

    @Override
    public MyPageInfo findBySearch(HttpServletRequest request) {
        int currentPageNum=1;  //当前页
        int pageSize=5;  //每页大小
        String currentPageNumStr = request.getParameter("pageNum");
        if (currentPageNumStr!=null){
            currentPageNum=Integer.parseInt(currentPageNumStr);
        }
        String title = request.getParameter("title");
        Page<Object> page = PageHelper.startPage(currentPageNum, pageSize);//必须在执行sql之前执行
        adminMapper.findBySearch(title);
        PageInfo<Object> pageInfo = page.toPageInfo();
        MyPageInfo myPageInfo = new MyPageInfo(pageInfo.getPageNum(),pageInfo.getPageSize(),
                pageInfo.getNavigateLastPage(),pageInfo.getTotal(),pageInfo.getList());
        myPageInfo.setSearch1(title);
        return myPageInfo;
    }

    @Override
    public void deleteById(int id) {
        adminMapper.deleteById(id);
    }

    @Override
    public MyPageInfo findBySearch1(HttpServletRequest request, int id) {
        int currentPageNum=1;  //当前页
        int pageSize=5;  //每页大小
        String currentPageNumStr = request.getParameter("pageNum");
        if (currentPageNumStr!=null){
            currentPageNum=Integer.parseInt(currentPageNumStr);
        }
        String title = request.getParameter("title");
        Page<Object> page = PageHelper.startPage(currentPageNum, pageSize);//必须在执行sql之前执行
        adminMapper.findBySearch1(title,id);
        PageInfo<Object> pageInfo = page.toPageInfo();
        MyPageInfo myPageInfo = new MyPageInfo(pageInfo.getPageNum(),pageInfo.getPageSize(),
        pageInfo.getNavigateLastPage(),pageInfo.getTotal(),pageInfo.getList());
        myPageInfo.setSearch1(title);
        return myPageInfo;
    }

    @Override
    public Reply findById(int id) {
        return adminMapper.findById(id);
    }

    @Override
    public void deleteById1(int id) {
      adminMapper.deleteById1(id);
    }

    @Override
    public MyPageInfo findBySearch2(HttpServletRequest request, String type, int id) {
        int currentPageNum=1;  //当前页
        int pageSize=5;  //每页大小
        String currentPageNumStr = request.getParameter("pageNum");
        if (currentPageNumStr!=null){
            currentPageNum=Integer.parseInt(currentPageNumStr);
        }
        String title = request.getParameter("title");
        Page<Object> page = PageHelper.startPage(currentPageNum, pageSize);//必须在执行sql之前执行
        adminMapper.findBySearch2(title,type,id);
        PageInfo<Object> pageInfo = page.toPageInfo();
        MyPageInfo myPageInfo = new MyPageInfo(pageInfo.getPageNum(),pageInfo.getPageSize(),
                pageInfo.getNavigateLastPage(),pageInfo.getTotal(),pageInfo.getList());
        myPageInfo.setSearch1(title);
        return myPageInfo;
    }

    @Override
    public Choice findById1(int id) {
        return adminMapper.findById1(id);
    }

    @Override
    public String doEdit(Reply reply) {

            adminMapper.create1(reply);
            return "ok";}

    @Override
    public MyPageInfo findBySearch3(HttpServletRequest request) {
        int currentPageNum=1;  //当前页
        int pageSize=5;  //每页大小
        String currentPageNumStr = request.getParameter("pageNum");
        if (currentPageNumStr!=null){
            currentPageNum=Integer.parseInt(currentPageNumStr);
        }
        String name = request.getParameter("name");
        String dizhi = request.getParameter("dizhi");
        Page<Object> page = PageHelper.startPage(currentPageNum, pageSize);//必须在执行sql之前执行
        adminMapper.findBySearch3(name,dizhi);
        PageInfo<Object> pageInfo = page.toPageInfo();
        MyPageInfo myPageInfo = new MyPageInfo(pageInfo.getPageNum(),pageInfo.getPageSize(),
                pageInfo.getNavigateLastPage(),pageInfo.getTotal(),pageInfo.getList());
        myPageInfo.setSearch1(name);
        return myPageInfo;
    }

    @Override
    public MyPageInfo findBySearch4(HttpServletRequest request) {
        int currentPageNum=1;  //当前页
        int pageSize=5;  //每页大小
        String currentPageNumStr = request.getParameter("pageNum");
        if (currentPageNumStr!=null){
            currentPageNum=Integer.parseInt(currentPageNumStr);
        }
        String name = request.getParameter("name");
        Page<Object> page = PageHelper.startPage(currentPageNum, pageSize);//必须在执行sql之前执行
        adminMapper.findBySearch4(name);
        PageInfo<Object> pageInfo = page.toPageInfo();
        MyPageInfo myPageInfo = new MyPageInfo(pageInfo.getPageNum(),pageInfo.getPageSize(),
                pageInfo.getNavigateLastPage(),pageInfo.getTotal(),pageInfo.getList());
        myPageInfo.setSearch1(name);
        return myPageInfo;
    }

    @Override
    public void findByIdty(int id) {
         adminMapper.fingByIdty(id);
    }

    @Override
    public Changest findByIdtp(int id) {
        return adminMapper.fingByIdtp(id);
    }

    @Override
    public void deleteById2(int id) {
        adminMapper.deleteById2(id);
    }

    @Override
    public int find(int id) {
        return adminMapper.find(id);
    }

    @Override
    public String jy(Contract contract) {
        Contract db=adminMapper.findjy(contract.getContent());
        if (db==null){
            adminMapper.createjy(contract);
            return "ok";
        }else{
            return "exist";
        }
    }
    @Override
    public void jydeleteById(int id) {
    adminMapper.jydelte(id);
    }

    @Override
    public void jyfindById(int cid) {
    adminMapper.jyfindById(cid);
    }

    @Override
    public int jyfind(int id) {
        return adminMapper.jyfind(id);
    }

    @Override
    public String findname(int id) {
        return adminMapper.findname(id);
    }

    @Override
    public void setname(String name,int id) {
        adminMapper.setname(name,id);
    }

    @Override
    public int fingcid(int id) {
        return adminMapper.fingcid(id);
    }


}


